var _ID_ = "LoanID"
var _TrackerRequestID_ = "TrackerRequestID"
var _TrReqID_ = "TrReqID"
var SERVICE_NOT_AVAILIBLE = "Sorry!\nPage is still loading.\n\nTry again.";
var ALOANERS = "al.afchome.net";
function features_MB(add) {
  var s = ",status=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes,top=0,left=0," + (add || '');
  var statusBar = s.match(/status=yes/i) ? true : false;
  return "height=" + (screen.availHeight - 30 - (statusBar ? 30 : 0)) + ",width=" + (screen.availWidth - 10) + s;
}

function showApplInfo(ev,params){
	try{
		var applID = typeof ev != 'object' ? ev : null;
		if( !applID ){
			var rs = getRsFromEvent(ev);
			if( rs )applID = rs(_ID_).Value;
		}
		if( !applID )applID = rsField(ev,_ID_).Value;
	}catch(e){}
	if( !applID )return alert("No "+_ID_+" was provided.");
	var menuParam = ""+(params||"");//applID ? "" : "&menuID="+MenuID;
	var url = "/Manager/Menu/ShowApplicant.aspx?ApplID="+applID;
	if( applID < 1000000 )url = "http://"+ALOANERS+url;
	var w =	top.open(url+menuParam,"APPLICATION_"+applID,features_MB("status=yes"));
	var count = 10;
	_setWindowFocus.proc = setInterval(function(){_setWindowFocus()},1000);
	//Local
	function _setWindowFocus(){
		try{w.focus();count=0;
		}catch(e){return;}
		if( !count ){
			//status = "Got Focus! "+_setWindowFocus.proc;
			return clearInterval(_setWindowFocus.proc);
		}
	}
}
function getRsFromEvent(ev){return ev.rs || rsFromElement(ev.srcElement);}
function pullCredit_Old(){
	if( !WS )return alert(SERVICE_NOT_AVAILIBLE) || false;
	var ret = WS(CO("PullCredit"));
	var error = showSOAPError(ret);
	if( error && !confirm("Send applicant to "+event.menuName+" anyway?") )return false;
	sendToMenu("","",error);
}
function pullCredit(){
	var ret = WS(CO("PullCredit"));
	if( showSOAPError(ret) )return false;
	fireCreditRepulled(ApplID());
	return true;
}

// onSelect__XXXXXXXX ----------------------------------------------------------
var onSelect__WINDOW = null;
function onSelect__CLOSE_WINDOW(w){
	if( onSelect__CLOSE_WINDOW.w )try{onSelect__CLOSE_WINDOW.w.close()}catch(e){};
	onSelect__CLOSE_WINDOW.w = w;
}
function onSelect__Tracker(ev){
	var rs = ev.rs || rsFromElement(ev.srcElement);
	showTracker(rs("Tracker").value);
}
function onSelect__TrReqID(ev){onSelect__VIN(ev)}
function onSelect__TrackerRequestID(ev){onSelect__VIN(ev)}
function onSelect__VIN(ev){
	var rs = ev.rs || rsFromElement(ev.srcElement);
	var fieldName = containsField(rs,_TrackerRequestID_) ? _TrackerRequestID_ : (containsField(rs,_TrReqID_) ? _TrReqID_ : "");
	if( !fieldName )return alert("No Tracker Request ID was provided.");
	var trID = rs(fieldName).value;
	if( !trID )return alert("No tracker request exists for this VIN.\nClick on LoanID to see account info.") || false;
	onSelect__CLOSE_WINDOW(showTrackerRequest(trID));
}
function onSelect__LoanID(ev){
	var rs = ev.rs || rsFromElement(ev.srcElement);
	onSelect__CLOSE_WINDOW(showApplInfo(rs(_ID_).value));
}
// InstantID   ---------------------------------------------------------------------
function showInstantID(ce){
	var loanID = typeof(ce) == "object" ? (ce.rs ? ce.rs(_ID_).Value : rsField(ce,_ID_).value) : ce;
	if( !parseInt(loanID) )return alert("No LoanID was provided.");
	var showParams = ["status=no","scrollbars=yes","resizable=yes"].join(",");
	var w = open("/manager/underwriting/InstantID.aspx?ApplID="+loanID,"INSTANTID"+loanID,showParams);
	return winsPush(w);
}
// Title   ---------------------------------------------------------------------
function showTitle(ce){
	var loanID = typeof(ce) == "object" ? (ce.rs ? ce.rs(_ID_).Value : rsField(ce,_ID_).value) : ce;
	if( !parseInt(loanID) )return alert("No LoanID was provided.");
	var showParams = ["status=no","resizable=yes"].join(",");
	var w = open("/manager/Title/TitleFromDMV.aspx?ApplID="+loanID,"TITLE"+loanID,showParams);
	return winsPush(w);
}
// Tracker ---------------------------------------------------------------------
function showTrackerForm(ce,vin){
	var loanID = ce && typeof(ce) == "object" ? (ce.rs ? ce.rs(_ID_).Value : rsField(ce,_ID_).value) : ce;
	if( !parseInt(loanID) )return alert("No LoanID was provided.");
	var showParams = ["status=yes","resizable=yes"].join(",");
	var w = open("/manager/Tracking/TrackingForm.aspx?ApplID="+loanID,"TRACKER"+loanID,showParams);
	return winsPush(w);
}
function showTrackerRequestDefault(el){
	return doOnSelect(el,null,showTrackerRequest);
}
function showTrackerRequest(trackerRequestID,vin){
	var search = vin ? "VIN="+vin : "";
	if( !vin ){
		if( typeof(trackerRequestID) == "object" ){
			var rs = trackerRequestID.rs || rsFromElement(trackerRequestID.tagName?trackerRequestID:trackerRequestID.srcElement);
			if( !containsField(rs,_TrackerRequestID_) )return alert("No Tracker Request ID was provided.");
			trackerRequestID = rs(_TrackerRequestID_).value;
		}
		search = "TrackerRequestID="+trackerRequestID;
	}
	var showParams = ["status=yes","resizable=yes","dialogWidth=400px","dialogHeight=300px"];
	var w = open("/manager/Tracking/TrackerRequest.aspx?"+search,"TRACKER_REQUEST_"+(vin||trackerRequestID),showParams);
	return winsPush(w);
}
function showAirCeptHistory(trackerID,locate){
	var ret = WS(cb,"AirCept_Fetch",trackerID,locate);
	// Local
	function cb(ret){
		if( showSOAPError(ret) )return;
		showModelessDialog("/include/ShowRS.htm?title="+trackerID,{rs:getRSfromDS(ret.value),search:false,onSelect:showTrackerOnMap},"resizable:1;status:0");
	}
}
function showTrackerOnMap(ev){
	var lo = ev.rs("longitude").value;
	var la = ev.rs("latitude").value;
	var top = ev.srcElement.document.parentWindow.screenTop;
	var left = ev.srcElement.document.parentWindow.screenLeft;
	showMap(lo,la,top,left);
}
function showTracker(trackerID){
	var showParams = ["status=no","resizable=yes","dialogWidth=100px","dialogHeight=100px"];
	//open("/manager/Tracking/Tracker.aspx?TrackerID="+trackerID)
	return w = showModelessDialog("/manager/Tracking/Tracker.aspx?TrackerID="+trackerID,{opener:top},showParams.join(";"))
}
function showMap(lo,la,top,left){
	var w = window.open("/maps/map.htm?lo="+lo+"&la="+la+"&zoom=2","MAP","resizable=1,status=0,width=400,height=400,top="+top+",left="+left);
	winsPush(w);
}
// Dealer Info ---------------------------------------------------------------------
function _BreakdownCode(v){
	var field = rsField();
	if( arguments.length )field.value = v;
	return field.value;
}
function _DealerSalesPersonID(v){
	var field = rsField();
	if( arguments.length )field.value = v;
	return field.value;
}
function _DealerPhone(v){
	var field = rsField();
	if( arguments.length )field.value = v;
	return field.value;
}
function _DealerFax(v){
	var field = rsField();
	if( arguments.length )field.value = v;
	return field.value;
}
// Borrower ------------------------------------------------------------------
